package com.mou.controller.user;


import com.mou.dto.*;
import com.mou.entity.Transaction;
import com.mou.result.Result;
import com.mou.service.ProductService;
import com.mou.service.TransactionService;
import com.mou.vo.CurrentOrderVO;
import com.mou.vo.MyOrderVO;
import com.mou.vo.TransactionVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController("userTransactionController")
@RequestMapping("/user/transaction")
@Slf4j
public class    TransactionController {

    @Autowired
    private TransactionService transactionService;
    @Autowired
    private ProductService productService;

    @PostMapping("/order/add")
    public Result<String> order(@RequestBody TransactionDTO transactionDTO) {
        log.info("用户下单，transactionDTO:{}", transactionDTO);
        Transaction transaction = new Transaction();
        BeanUtils.copyProperties(transactionDTO, transaction);
        transactionService.add(transaction);
        return Result.success();
    }

    @PostMapping("/order/detail")
    public Result<TransactionVO> orderDetail(@RequestBody TransactionOrderDTO transactionOrderDTO) {
        log.info("用户下单，TransactionOrderDTO:{}", transactionOrderDTO);
        TransactionVO orderDetail = transactionService.getOrderDetail(transactionOrderDTO);
        return Result.success(orderDetail);
    }

    /**
     *
     * 查询自己的订单,自己是买家所有订单
     * @param myOrderDTO
     * @return
     */
    @PostMapping("/myOrder")
    public Result<List<MyOrderVO>> order(@RequestBody MyOrderDTO myOrderDTO) {
        log.info("查询我的订单，MyOrderDTO:{}", myOrderDTO);
        List<MyOrderVO> myOrderVOs = transactionService.getMyOrder(myOrderDTO);
        return Result.success(myOrderVOs);
    }
    @PostMapping("/otherOrder")
    public Result<List<MyOrderVO>> otherOrder(@RequestBody OtherOrderDTO otherOrderDTO) {
        log.info("查询其他人的订单，OtherOrderDTO:{}", otherOrderDTO);
        List<MyOrderVO> myOrderVOs = transactionService.getOtherOrder(otherOrderDTO);
        return Result.success(myOrderVOs);
    }

    @PostMapping("/order/cancel")
    public Result<String> cancel(@RequestBody CancelDTO cancelDTO){
        transactionService.cancel(cancelDTO.getId());
        return Result.success();
    }
    @PostMapping("/order/accept")
    public Result<String> accept(@RequestBody AcceptDTO acceptDTO){
        transactionService.accept(acceptDTO.getId());
        return Result.success();
    }

    @PostMapping("/order/startDelivery")
    public Result<String> startDelivery(@RequestBody DeliveryDTO deliveryDTO){
        transactionService.startDelivery(deliveryDTO.getId());
        return Result.success();
    }

    @PostMapping("/order/confirmDelivery")
    public Result<String> confirmDelivery(@RequestBody ConfirmDeliveryDTO confirmDeliveryDTO){
        transactionService.confirmDelivery(confirmDeliveryDTO.getId());
        return Result.success();
    }

    @GetMapping("/order/current")
    public Result<CurrentOrderVO> current(@RequestBody CurrentOrderDTO currentOrderDTO){
        Transaction transaction =transactionService.getById(currentOrderDTO.getId());
        CurrentOrderVO myOrderVOs = new CurrentOrderVO();
        myOrderVOs.setId(transaction.getId());
        Double price = productService.getById(transaction.getProductId()).getPrice();
        myOrderVOs.setAmount(transaction.getQuantity()*price);
        return Result.success(myOrderVOs);
    }

//    @PostMapping("/toUnConfirmOrder")
//    public Result<List<MyOrderVO>> toConfirmOrder(@RequestBody TransactionOrderDTO transactionOrderDTO){
//        List<MyOrderVO> myOrderVOs =transactionService.getUnConfirmOrder(transactionOrderDTO);
//        return Result.success(myOrderVOs);
//    }



}
